﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class KaoGuanWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client1 = new EmpbasicService.EmpBasicServiceClient();
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private KaoGuanModel model = new KaoGuanModel();
        public bool IsNew = false;
        public int ID;
        public KaoGuanWindow(int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            IsNew = isnew;
            //Client1.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            //Client1.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            if (IsNew)
            {
                this.Title = "新增考官信息";
                model = new KaoGuanModel();
                this.DataContext = model;
                //nian.SelectedIndex = 0;
            }
            else
            {
                this.Title = "编辑考官信息";
                Client.GetKaoGuanModelCompleted += new EventHandler<GetKaoGuanModelCompletedEventArgs>(Client_GetKaoGuanModelCompleted);
                Client.GetKaoGuanModelAsync("", ID,"0","选调交流");
            }
        }

        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            //if (e.Result != "人事干部")
            //{
            //    leibie1.Visibility = System.Windows.Visibility.Visible;
            //    leibie2.Visibility = System.Windows.Visibility.Visible;
            //}
            //else
            //{
            //    leibie1.Visibility = System.Windows.Visibility.Collapsed;
            //    leibie2.Visibility = System.Windows.Visibility.Collapsed;
            //}
        }

        void Client_GetKaoGuanModelCompleted(object sender, GetKaoGuanModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
            if (model.IsKaoGuanZheng = true) check.IsChecked = true; else check.IsChecked = false;
            //nian.SelectedIndex = DateTime.Today.Year - model.Year;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(model.YongRenDanWei))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入用人单位！");
                return;
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入姓名！");
                return;
            }
            if (string.IsNullOrEmpty(model.CardNo))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入身份证号！");
                return;
            }
            if(!string.IsNullOrEmpty(model.RiQi.ToString()) && model.RiQi != Convert.ToDateTime("1900-01-01"))
            {
                if(Convert.ToDateTime(model.RiQi).AddYears(3)<DateTime.Today)
                {
                    model.Memo += "\r\n考官证已过期";
                }
            }
            if (IsNew == true)
            {
                //model.Year = DateTime.Today.Year - nian.SelectedIndex;
                model.Creator = Session.LoginUser.LoginName;
                model.Type = "选调交流";
                if (check.IsChecked == true) model.IsKaoGuanZheng = true; else model.IsKaoGuanZheng = false;
                Client.AddKaoGuanModelCompleted += new EventHandler<AddKaoGuanModelCompletedEventArgs>(Client_AddKaoGuanModelCompleted);
                Client.AddKaoGuanModelAsync(model);
            }
            else
            {
                //model.Year = DateTime.Today.Year - nian.SelectedIndex;
                if (check.IsChecked == true) model.IsKaoGuanZheng = true; else model.IsKaoGuanZheng = false;
                Client.UpdateKaoGuanModelCompleted += new EventHandler<UpdateKaoGuanModelCompletedEventArgs>(Client_UpdateKaoGuanModelCompleted);
                Client.UpdateKaoGuanModelAsync(model);
            }
        }

        void Client_UpdateKaoGuanModelCompleted(object sender, UpdateKaoGuanModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void Client_AddKaoGuanModelCompleted(object sender, AddKaoGuanModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
